﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using YDCommon;
using YDModel;
using YDModel.Entity;
using YDServices.Comm;

namespace YDServices
{
    public class EmployeeService : BaseService
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="conds"></param>
        /// <returns></returns>
        public List<T_Employee> getList(ConditionItem[] conds)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var list = db.T_Employee.Where(o => o.State != false).AsQueryable();
                    if (conds != null)
                    {
                        var qb = QueryBuilder<T_Employee>.Create().AddConditions(conds);
                        list = list.Where(qb.ToExpression());
                    }
                    List<T_Employee> empList = list.ToList();
                    //添加角色名称
                    var userRoleList = db.T_User_Role.Join(db.T_Role,s=>s.Role_ID,o=>o.ID,(o,s)=>
                        new {
                                RoleName = s.RoleName,
                                ID=o.ID,
                                Role_ID=o.Role_ID,
                                User_ID=o.User_ID
                            }
                    ).ToList(); 
                    foreach (var user in empList)
                    {
                        var role = userRoleList.Where(o => o.User_ID == user.ID).FirstOrDefault();
                        if (role != null)
                        {
                            user.Role_Name = role.RoleName;
                        }
                    }

                    return empList;
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="tu"></param>
        public void AddItem(T_Employee tu, T_User loginUser)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var Employee = db.T_Employee.Where(o => o.EmployeeName == tu.EmployeeName && o.State == true).FirstOrDefault();
                    if (Employee != null)
                    {
                        throw new ApplicationException("员工已存在!");
                    }

                    tu.State = true;
                    tu.CreatedTime = GetSqlNow();
                    tu.CreatedBy = "[" + loginUser.ID + "]" + loginUser.UserName;
                    db.T_Employee.Add(tu);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="tu"></param>
        public void EditItem(T_Employee tu, T_User loginUser)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var item = db.T_Employee.Where(o => o.ID == tu.ID && o.State == true).FirstOrDefault();
                    if (item == null)
                    {
                        throw new ApplicationException("员工信息不存在!");
                    }
                    item.EmployeeName = tu.EmployeeName;
                    item.UserCode = tu.UserCode;

                    item.ModifiedTime = GetSqlNow();
                    item.ModifiedBy = "[" + loginUser.ID + "]" + loginUser.UserName;

                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="tu"></param>
        /// <param name="loginEmployee"></param>
        public void DelItem(T_Employee item)  //List<T_Employee> tuList 
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {

                    var fact = db.T_Employee.Where(o => o.ID == item.ID).FirstOrDefault();
                    if (fact == null)
                    {
                        throw new ApplicationException("员工信息不存在!");
                    }
                    fact.State = false;
                    fact.DeletedTime = GetSqlNow();
                    fact.DeletedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;

                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="tu"></param>
        public T_Employee LoginEmployee(T_Employee tu)
        {
            try
            {

                using (YDDataEntities db = new YDDataEntities())
                {
                    var Employee = db.T_Employee.Where(o => o.UserCode == tu.UserCode && o.State == true).FirstOrDefault();
                    if (Employee == null)
                    {
                        throw new ApplicationException("账号不存在!");
                    }

                    if (!Utils.Encrypt(tu.UserPassword).Equals(Employee.UserPassword))
                    {
                        throw new ApplicationException("密码不正确!");
                    }
                    T_User user = new T_User();
                    user.ID = Employee.ID;
                    user.UserName = Employee.EmployeeName;

                    Global.loginUser = user;
                    return Employee;

                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message.ToString());
                //Errors.Add(ex);
                //WriteException(ex);
                //return null;
            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="tu"></param>
        /// <returns></returns>
        public void EditPassword(T_Employee tu,string oldPassword)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var item = db.T_Employee.Where(o => o.ID == tu.ID && o.State == true).FirstOrDefault();
                    if (item == null)
                    {
                        throw new ApplicationException("员工信息不存在!");
                    } 

                    //判断旧密码是否正确
                    if(!item.UserPassword.Equals(oldPassword))
                    {
                        throw new ApplicationException("旧密码输入不正确!");
                    }
                    item.UserPassword = tu.UserPassword;

                    item.ModifiedTime = GetSqlNow();
                    item.ModifiedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;

                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex); 
            }

        }

        /// <summary>
        /// 角色设置
        /// </summary>
        /// <param name="tu"></param>
        public void EditRole(T_User_Role userRole)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    long userid = userRole.User_ID.Value;
                    var item = db.T_User_Role.Where(o => o.User_ID== userid).FirstOrDefault();
                    if (item != null)
                    {
                        db.T_User_Role.Remove(item);
                    } 
                    db.T_User_Role.Add(userRole); 

                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }

         

    }
}
